package com.cuz.hot100;

public class Q37AppearanceSequence {
    public static void main(String[] args) {
        System.out.println(countAndSay1(4));
    }
    public static String countAndSay1(int n) {
        if (n < 1) {
            return "";
        }
        if (n == 1) {
            return "1";
        }
        String str = "1";
        for (int index = 2; index <= n; index++) {
            str=process(str);
        }
        return str;
    }



    private static String process(String str) {
        char[] charArray = str.toCharArray();
        StringBuilder sb = new StringBuilder();
        int curChar = charArray[0];
        int curCount = 1;
        for (int i = 1; i < charArray.length; i++) {
            if (charArray[i] == curChar) {
                curCount++;
            } else {
                sb.append(curCount).append((char) curChar);
                curChar = charArray[i];
                curCount = 1;
            }
        }
        sb.append(curCount).append((char) curChar);
        return sb.toString();
    }

}
